<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\PermissionRequest;
use App\Permission;

class PermissionController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * 获取权限列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $permissions = Permission::all();
        return response()->json($permissions);
    }

    /**
     * 新增权限
     * @param PermissionRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(PermissionRequest $request)
    {
        $permission = Permission::updateOrCreate(['id' => $request->get('id', 0)], $request->all());
        return response()->json($permission);
    }

    /**
     * 删除权限
     * @param Request $request
     * @param Permission $permission
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request, Permission $permission)
    {
        $permission->delete();
        return response()->json(['status' => 200, 'message' => 'success']);
    }

    /**
     * 权限详情
     * @param Request $request
     * @param Permission $permission
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request, Permission $permission)
    {
        return response()->json($permission);
    }
}
